﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;

public partial class _Default : System.Web.UI.Page
{
    DataClassesDataContext data = new DataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckExpireDate();
        PopulateRepeter();
        lblNoResult.Visible = false;
        Counter();
    }

    public void PopulateRepeter()
    {
        var db = new DataClassesDataContext();

        var qry = (from x in db.JobOffers
                   join c in db.JobStatus on x.JobStatus equals c.StatusId
                   join k in db.JobCategories on x.JobCategory equals k.JobCategoryId
                   where x.JobStatus == 1
                   orderby x.JobCreated descending
                   select x).ToList();
        rptJobOffers.DataSource = qry;
        rptJobOffers.DataBind();
    }

    public static string CutDateString(string text)
    {
        if (text.Length > 9)
        {
            text = text.Substring(0, 9);
        }
        return text;
    }

    public void Counter()
    {
        int count = (from x in data.JobOffers
                     where x.JobStatus1.StatusName.Equals("Aktiv")
                     select x.JobId).Count();
        lblCounter.Text = count.ToString();
    }


    public void CheckExpireDate()
    {
        DateTime date = DateTime.Now;

        var qry = (from x in data.JobOffers
            where x.JobExpire <= date
            select x).ToList();
        foreach (var u in qry)
        {
            u.JobStatus = 2;
        }
        try
        {
            data.SubmitChanges();
        }
        catch (Exception e)
        {

            Console.WriteLine(e);
        }
    }

    protected void Unnamed1_Click(object sender, EventArgs e)
    {
        var db = new DataClassesDataContext();
        var qry = (from x in db.JobOffers
                   where x.JobTitle.Contains(txtSearch.Text) && x.JobStatus == 1
                   select x).ToList();

        lblSearchMessage.Text = "Úrslit av leitingini: " + txtSearch.Text;
        if (qry.Count < 1)
        {
            ShowHide.Visible = false;
            lblNoResult.Visible = true;
            lblNoResult.Text = "Einki var funnið";
        }
        else
        {
            ShowHide.Visible = true;
            rptJobOffers.DataSource = qry;
            rptJobOffers.DataBind();
            lblNoResult.Visible = false;
            
        }

    }


}